/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.shared.compress;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author Kristopher T Babic
 */
public class ZipCompress extends Compress {

    public ZipCompress() {
        super();
    }

    public ZipCompress(String data) {
        super(data);
    }

    private String stored;

    public void compress() throws IOException {
/*		
		ByteArrayOutputStream finalOut = new ByteArrayOutputStream();
		String temp = getRaw();
		setDecompressed(getRaw());
		
	    ZipOutputStream out = new ZipOutputStream(finalOut);
		ZipEntry entry = new ZipEntry("InterDraw");
		byte[] b = temp.getBytes();
		out.putNextEntry(entry);
		out.write(b, 0, temp.length());
		out.finish();
		
		setCompressed(finalOut.toString());


	ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
System.out.println(">>>>"+getRaw()+"<<<<<<");
	String temp = getRaw();//new String("this is a test");//getRaw();//new String("this is yet another test string to figure out if I can send compressed data.");
    ZipOutputStream out = new ZipOutputStream(byteArray);
	ZipEntry entry = new ZipEntry("test");
	byte[] b = temp.getBytes();
	out.putNextEntry(entry);
	out.write(b, 0, temp.length());
	out.finish();
	
	setCompressed(byteArray.toString());
System.out.println("<<<<<<<"+byteArray.size()+">>>>>>>>");System.out.flush();
	stored = byteArray.toString();	*/

        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        String temp = getRaw();//new String("this is yet another test string to figure out if I can send compressed data.");
        ZipOutputStream out = new ZipOutputStream(byteArray);
        ZipEntry entry = new ZipEntry("test");
        byte[] b = temp.getBytes();
        out.putNextEntry(entry);
        out.write(b, 0, temp.length());
        out.finish();

        setCompressed(byteArray.toString());

    }

    public void decompress() throws IOException {
/*	
		ByteArrayInputStream input = new ByteArrayInputStream(getRaw().getBytes());
		ZipInputStream in = new ZipInputStream(input);
		ZipEntry z;

		String finalOut = new String(); 		
		while ((z = in.getNextEntry()) != null){

			BufferedReader zin = new BufferedReader (new InputStreamReader(in));
		
			String s;
			while ((s = zin.readLine()) != null)
				finalOut += s;
				
			in.closeEntry();
		}
		in.close();	
		
		setDecompressed(finalOut);	

System.out.println("//////"+getRaw().length()+"\\\\\\");
	ByteArrayInputStream b2 = new ByteArrayInputStream(getRaw().getBytes());
					String s = new String();	
				ZipInputStream in = new ZipInputStream(b2);//System.in);
				ZipEntry z;
				while ((z = in.getNextEntry()) != null){
					System.out.println("in while:"+z);System.out.flush();
					//System.out.println(z.getName()+":");System.out.flush();
					BufferedReader zin = new BufferedReader (new InputStreamReader(in));
					System.out.println("after create reader");System.out.flush();
					String t = new String();
					while ((t = zin.readLine()) != null){
						s += t; System.out.println("second while");System.out.flush();}
					in.closeEntry();
				}
				in.close();
	
		setDecompressed(s);	
	}
*/

        //System.out.println("<<"+output+">>\n"+">>"+f2+"<<");
//ByteArrayInputStream byteArray = new ByteArrayInputStream(getRaw().getBygettes());	
        ByteArrayInputStream b2 = new ByteArrayInputStream(getRaw().getBytes());
        String s = new String();
        ZipInputStream in = new ZipInputStream(b2);//System.in);
        ZipEntry z;
        while ((z = in.getNextEntry()) != null) {

            //System.out.println(z.getName()+":");System.out.flush();
            BufferedReader zin = new BufferedReader(new InputStreamReader(in));
            String t;
            while ((t = zin.readLine()) != null)
                s += t + '\n';//System.out.println(s);System.out.flush();
            in.closeEntry();
        }
        in.close();

        setDecompressed(s);
    }
}